
package com.coolgor.coldot.service.stock.impl;

import com.coolgor.coldot.common.PageCalculator;
import com.coolgor.coldot.entity.stock.MarketDaily;
import com.coolgor.coldot.dao.stock.MarketDailyDao;
import com.coolgor.coldot.dto.stock.MarketDailyExecution;
import com.coolgor.coldot.enums.stock.MarketDailyOperationStateEnum;
import com.coolgor.coldot.exceptions.stock.MarketDailyOperationException;
import com.coolgor.coldot.service.stock.MarketDailyService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


@Service
public class MarketDailyServiceImpl implements MarketDailyService {

	private static Logger log = LoggerFactory.getLogger(MarketDailyServiceImpl.class);

	@Autowired
	MarketDailyDao marketDailyDao;

	@Override
	public MarketDailyExecution getByID(Long recordID) {
		// 空值判断
		if (null == recordID || recordID.equals("")) {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.NULL_ID);
		}
		MarketDaily marketDaily = marketDailyDao.queryByID(recordID);
		if (marketDaily != null) {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.SUCCESS, marketDaily);
		} else {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.NULL);
		}
	}

	@Override
	public MarketDailyExecution getList(MarketDaily marketDailyCondition, int pageIndex, int pageSize) {
		// 将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		// 依据查询条件，调用dao层返回相关的查询结果列表
		List<MarketDaily> list = marketDailyDao.queryList(marketDailyCondition, rowIndex, pageSize);
		// 依据相同的查询条件，返回查询结果总数
		int count = marketDailyDao.queryCount(marketDailyCondition);
		MarketDailyExecution result = new MarketDailyExecution();
		if (list != null) {
			result.setState(MarketDailyOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(MarketDailyOperationStateEnum.SUCCESS.getStateInfo());
			result.setMarketDailyList(list);
			result.setCount(count);
		} else {
			result.setState(MarketDailyOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public MarketDailyExecution add(MarketDaily marketDaily) throws MarketDailyOperationException {
		// 空值判断
		if (marketDaily == null) {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行插入数据操作 
			effectedNum = marketDailyDao.insert(marketDaily);
			if (effectedNum <= 0) {
				throw new MarketDailyOperationException("MarketDaily信息插入失败");
			}
		} catch (Exception e) {
			throw new MarketDailyOperationException("Add marketDaily error:" + e.getMessage());
		}
		return new MarketDailyExecution(MarketDailyOperationStateEnum.SUCCESS, marketDaily);
	}

	@Override
	@Transactional
	public MarketDailyExecution modify(MarketDaily marketDaily) throws MarketDailyOperationException {
		// 空值判断
		if (marketDaily == null) {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行修改更新操作 
			effectedNum = marketDailyDao.update(marketDaily);
			if (effectedNum <= 0) {
				throw new MarketDailyOperationException("MarketDaily信息更新失败");
			}
		} catch (Exception e) {
			throw new MarketDailyOperationException("Update marketDaily error:" + e.getMessage());
		}
		return new MarketDailyExecution(MarketDailyOperationStateEnum.SUCCESS, marketDaily);
	}

	@Override
	@Transactional
	public MarketDailyExecution remove(Long recordID) throws MarketDailyOperationException{
		// 空值判断
		if (recordID == null) {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = marketDailyDao.delete(recordID);
			if (effectedNum <= 0) {
				throw new MarketDailyOperationException("MarketDaily单条信息删除失败");
			}
		} catch (Exception e) {
			throw new MarketDailyOperationException("Delete marketDaily error:" + e.getMessage());
		}
		return new MarketDailyExecution(MarketDailyOperationStateEnum.SUCCESS);
	}

	@Override
	@Transactional
	public MarketDailyExecution remove(MarketDaily marketDaily) throws MarketDailyOperationException{
		// 空值判断
		if (marketDaily == null) {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = marketDailyDao.delete(marketDaily.getRecordID());
			if (effectedNum <= 0) {
				throw new MarketDailyOperationException("MarketDaily单条信息删除失败");
			}
		} catch (Exception e) {
			throw new MarketDailyOperationException("Delete marketDaily error:" + e.getMessage());
		}
		return new MarketDailyExecution(MarketDailyOperationStateEnum.SUCCESS, marketDaily);
	}

	@Override
	@Transactional
	public MarketDailyExecution removeBatchByID(List<Long> recordIDList) throws MarketDailyOperationException{
		// 空值判断
		if (recordIDList == null || recordIDList.size() < 1) {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = marketDailyDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new MarketDailyOperationException("MarketDaily多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new MarketDailyOperationException("Batch Delete marketDaily error:" + e.getMessage());
		}
		MarketDailyExecution exe = new MarketDailyExecution(MarketDailyOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	@Transactional
	public MarketDailyExecution removeBatch(List<MarketDaily> marketDailyList) throws MarketDailyOperationException{
		// 空值判断
		if (marketDailyList == null) {
			return new MarketDailyExecution(MarketDailyOperationStateEnum.NULL);
		} 
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<marketDailyList.size(); i++) {
			recordIDList.add(marketDailyList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = marketDailyDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new MarketDailyOperationException("MarketDaily多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new MarketDailyOperationException("Batch Delete marketDaily error:" + e.getMessage());
		}
		MarketDailyExecution exe = new MarketDailyExecution(MarketDailyOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

}
